#include "mysql.h"
#include <QDebug>

MySql::MySql() {

}

bool MySql::initsql() { //初始化并建立一个数据库连接
	//连接数据库
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
	db.setHostName("localhost");
    db.setDatabaseName("mysql.db");
	db.setUserName("root");
	db.setPort(3306);
	db.setPassword("123456789");

	if (db.open()) {
		qDebug() << "数据库连接成功,666666!" << endl;
        QStringList tablelist = db.tables();
        query = new QSqlQuery(db);
        qDebug() << tablelist;
        if (!(tablelist.contains("user"))){
            QString createusers =
                    "CREATE TABLE user (Name text, status text, Passwords text, Mail1 text, Mail2 text, Mail3 text, CIP text, CPort text)";
            if (query->prepare(createusers)){
                if (query->exec()){
                    qDebug() << "Create user OJBK!";
                }
            } else {
                qDebug() << "Create table command error!";
            }
        } else {
            qDebug() << "table:user already exist";
        }
	} else {
		qDebug() << "数据库连接失败,伞兵!" << endl;
		QMessageBox::warning(NULL, "error", "数据库连接失败");
		return 0;
	}








}


int MySql::Login(QString Name, QString Password) {
	QString str1, str2;
	query = new QSqlQuery;
    query->exec(QString("select Passwords,status from user where Name='%1'").arg(Name));
    while (query->next()) {
        str1 = query->value(0).toString(); //密码
        str2 = query->value(1).toString(); //在线状态
        qDebug() << str1 << str2;
    }
	qDebug() << str1 << str2 << endl;
//    qDebug()<<"数据库连接成功,666666!"<<endl;
	if (str2 == NULL)  return 3; //用户不存在
	else if (str2 == '1')  return 2; //用户已经在线
	else if ((str2 == '0') && (str1 == Password)) return 1; //用户名和密码都正确
	else if ((str2 == '0') && (str1 != Password)) return 0; //密码错误
}


bool MySql::Regist(QString Name, QString Password, QString Mail,QString headpic) {
    char status = '0', Mail3 = '0', CIP = '0', CPort = '0';
    QString str = QString("select * from user where Name='%1'").arg(Name);
	query = new QSqlQuery;
    qDebug() << query->exec(str);
	query->last();
    qDebug() << query->at();
    int record = query->at() + 2;
    qDebug() << record;
	if (record != 0)
        return false;
    str = QString("INSERT INTO user VALUES('%1','%2','%3','%4','%5','%6','%7','%8')").arg(Name).arg(status).arg(Password).arg(Mail).arg(headpic).arg(Mail3).arg(CIP).arg(CPort);
    //query->prepare(str);
    bool ret = query->exec(str);//数据模型重新查询数据，更新tableView显示
    qDebug() << "到达数据库注册!" << str << ret << endl;
	return ret;
}

bool MySql::saveInformation(QString Name, QString Password, QString Mail,QString headpic) {
    QString str = QString("update user set Passwords='%2',Mail1='%3',Mail2='%4' where Name='%1'").arg(Name).arg(Password).arg(Mail).arg(headpic);
    query = new QSqlQuery;
    qDebug() << query->exec(str);
    return true;
}


int MySql::findpwd(QString Name, QString Mail) { //找回密码
	QString str1, str2, str3;
	query = new QSqlQuery;
    query->exec(QString("select Mail from user where Name='%1'").arg(Name));
	while (query->next()) {
		str1 = query->value(0).toString(); //邮件
	}
	if ((str1 == Mail) /*&& (str2 == question) && (str3 == answer)*/) return 3; //信息正确
	else if (str1 == NULL)   return 4; //用户不存在
	else  return 2;  //邮件错误
}

int MySql::findinf(QString Name) { //找到个人信息
    QString str1;
    query = new QSqlQuery;
    query->exec(QString("select Name from user where Name='%1'").arg(Name));
    while (query->next()) {
        str1 = query->value(0).toString(); //用户名
    }
    if (str1 == Name) return 3; //信息正确
    else return 4; //用户不存在
}
